using System.Collections.Generic;

namespace XFramework
{
    public class CodeGen
    {
        private static readonly Dictionary<string, ICodeGenTemplate> templates =
            new Dictionary<string, ICodeGenTemplate>();

        public static void RegisterTemplate(string templateName, ICodeGenTemplate codeGenTemplate)
        {
            if (templates.ContainsKey(templateName))
            {
                templates[templateName] = codeGenTemplate;
            }
            else
            {
                templates.Add(templateName, codeGenTemplate);
            }
        }

        private static ICodeGenTemplate GetTemplate(string templateName)
        {
            return templates.TryGetValue(templateName, out var template) ? template : null;
        }

        public static void Generate(IBindGroup bindGroup)
        {
            var task = GetTemplate(bindGroup.TemplateName).CreateTask(bindGroup);
            Generate(task);
        }

        private static void Generate(CodeGenTask task)
        {
            CodeGenPipeline.Default.Generate(task);
        }
    }
}